package utile

import (
	"crypto/md5"
	"fmt"
	"log"
	"net"
	"strconv"
)

func InArray(items []string, item string) bool {
	for _, eachItem := range items {
		if eachItem == item {
			return true
		}
	}
	return false
}

func ReturnData(code int, message string, data ...interface{}) interface{} {
	if len(data) <= 0  || (len(data) > 0 && data[0] == nil) {
		return map[string]interface{}{"code": code, "message": message, "data": ""}
	}
	return map[string]interface{}{"code": code, "message": message, "data": data[0]}
}

func Md5(str string) string {
	return fmt.Sprintf("%x", md5.Sum([]byte(str)))
}

func Str2Int(str string) int {
	num, err := strconv.Atoi(str)
	if err != nil {
		return 0
	}
	return num
}

func Int2Str(num int) string {
	return strconv.Itoa(num)
}

func GetOutboundIP() net.IP {
	conn, err := net.Dial("udp", "8.8.8.8:80")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	localAddr := conn.LocalAddr().(*net.UDPAddr)
	return localAddr.IP
}


//首字母转大写
func strFirstToUpper(str string) string {
	if len(str) < 1 {
		return ""
	}
	strArry := []rune(str)
	if strArry[0] >= 97 && strArry[0] <= 122 {
		strArry[0] -= - 32
	}
	return string(strArry)

}